package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFile;
import com.hzdesk.media.koreabatchmatvector.inter.OnItemClickListener;
import com.hzdesk.media.koreabatchmatvector.utils.DateUtil;

import java.util.List;

/**
 * Created by Administrator on 2018/6/20.
 * 杭州席媒科技有限公司  Developers  刘传华  09
 * 佛祖镇楼 bug辟易
 */

public class MyVideoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private OnItemClickListener mOnItemClickListener = null;
    private List<VideoFile> mList = null;
    private Context mContext;
    private int NORMAL_TYPE_VIEW = 0;
    private int FOOT_TYPE_VIEW = 1;
    static final int LOADING_MORE = 1;                                                              //正在加载更多
    static final int NO_MORE = 2;                                                                   //没有更多
    int footer_state = 1;                                                                           //脚布局当前的状态,默认为没有更多

    public MyVideoListAdapter(Context context) {
        this.mContext = context;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NORMAL_TYPE_VIEW) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_list_item, parent, false);
            MyHolder mMyHolder = new MyHolder(view);
            return mMyHolder;
        } else if (viewType == FOOT_TYPE_VIEW) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cloud_list_foot_item, parent, false);
            FootViewHodler footViewHodler = new FootViewHodler(view);
            return footViewHodler;
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyHolder) {
            final MyHolder myHolder = (MyHolder) holder;
            if (position == 0) {
                myHolder.videoLietItemLin.setBackground(mContext.getResources().getDrawable(R.drawable.mouse_list_top_bg));
            }
            if (position == (mList.size() - 1)) {
                myHolder.videoLietItemLin.setBackground(mContext.getResources().getDrawable(R.drawable.mouse_list_bootom_bg));
            }
            if (mList.get(position).getProgress()>=100||mList.get(position).getProgress()==0){
                myHolder.videoListItemTextViewProgress.setVisibility(View.GONE);
            }else {
                myHolder.videoListItemTextViewProgress.setVisibility(View.VISIBLE);
                myHolder.videoListItemTextViewProgress.setText(mList.get(position).getProgress()+"%");
            }
            myHolder.videoListItemProgress.setProgress(mList.get(position).getProgress());
            myHolder.videoListItemName.setText(mList.get(position).getName());
            myHolder.videoListItemTime.setText(DateUtil.dataToTimeString(mList.get(position).getCreatetime()));
            myHolder.videoListItemIamgePlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemPlay(mList.get(position));
                    }
                }
            });
            myHolder.videoListItemIamgeDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemDelete(mList.get(position));
                    }
                }
            });
            if (mList.get(position).getIsUpload()){

            }
            myHolder.videoListItemImageviewUpload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        myHolder.videoListItemProgress.setVisibility(View.VISIBLE);
                        myHolder.videoListItemTextViewProgress.setVisibility(View.VISIBLE);
                        mOnItemClickListener.onItemUpload(mList.get(position), position);
                    }
                }
            });
        } else if (holder instanceof FootViewHodler) {
            FootViewHodler footViewHodler = (FootViewHodler) holder;
            footShow(position, footViewHodler);
        }


    }

    public void addItemDate(List<VideoFile> mbean) {
        mList = mbean;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() + 1 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        if (position + 1 == getItemCount()) {
            return FOOT_TYPE_VIEW;
        } else {
            return NORMAL_TYPE_VIEW;
        }
    }

    /**
     * 改变脚布局的状态的方法,在activity根据请求数据的状态来改变这个状态
     *
     * @param state
     */
    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();
    }

    // 展示footView 的信息
    private void footShow(int position, FootViewHodler footViewHodler) {
        if (position == 0) {
            footViewHodler.item_rl.setVisibility(View.GONE);
        }
        switch (footer_state) {
            case LOADING_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText("加载中...");
                break;
            case NO_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.GONE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText("----地主家也没余粮了----");
                break;
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView videoListItemName;
        TextView videoListItemTime;
        ImageView videoListItemIamgePlay;
        ImageView videoListItemIamgeDelete;
        ImageView videoListItemImageviewUpload;
        LinearLayout videoLietItemLin;
        ProgressBar videoListItemProgress;
        TextView videoListItemTextViewProgress;

        public MyHolder(View itemView) {
            super(itemView);
            videoListItemProgress = (ProgressBar) itemView.findViewById(R.id.video_liet_item_progressBar);
            videoListItemName = (TextView) itemView.findViewById(R.id.video_list_item_name);
            videoListItemTime = (TextView) itemView.findViewById(R.id.video_list_item_time);
            videoListItemTextViewProgress = (TextView) itemView.findViewById(R.id.video_liet_item_textview_progress);
            videoListItemIamgePlay = (ImageView) itemView.findViewById(R.id.video_list_item_iamge_play);
            videoListItemIamgeDelete = (ImageView) itemView.findViewById(R.id.video_list_item_iamge_delete);
            videoListItemImageviewUpload = (ImageView) itemView.findViewById(R.id.video_list_item_imageview_upload);
            videoLietItemLin = (LinearLayout) itemView.findViewById(R.id.video_liet_item_lin);
        }
    }

    class FootViewHodler extends RecyclerView.ViewHolder {

        private final TextView tv_loding;
        private final ProgressBar item_pg;
        private final RelativeLayout item_rl;

        public FootViewHodler(View itemView) {
            super(itemView);
            tv_loding = itemView.findViewById(R.id.cloud_list_foot_item_tv_loding);
            item_pg = itemView.findViewById(R.id.cloud_list_foot_item_pg);
            item_rl = itemView.findViewById(R.id.cloud_list_foot_item_rl);
        }
    }

}
